package view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import snsdbook.engine.ClientEngine;
import util.Util;

public class ClientGUI extends JFrame {
  private static final long serialVersionUID = 1L;
  private JPasswordField pwdPassword;
  private JTextField txtUsername;
  private JTextArea txtaLogInfo;
  private ClientEngine engine_;
  private JButton btnBecomeABieber;
  private JButton btnChangeSecretFolder;
  
  public void init() {
    setSize(600, 600);
    Util.RegisterGUI(this);
    
    engine_ = new ClientEngine(
        JOptionPane.showInputDialog(
            "Please enter server address", "localhost"),
        JOptionPane.showInputDialog(
            "Please enter port address", "8100")
        );
    
    ChangeSecretFolderClicked();
    
    UpdateGUI();
  }

  /**
   * Create the applet.
   */
  public ClientGUI() {
    super("BIEBOX");
    
    GridBagLayout gridBagLayout = new GridBagLayout();
    gridBagLayout.columnWidths = new int[]{0, 0, 0};
    gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
    gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
    gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
    getContentPane().setLayout(gridBagLayout);
    JLabel lblLogo = new JLabel(new ImageIcon("logo.gif"));
    GridBagConstraints gbc_lblLogo = new GridBagConstraints();
    gbc_lblLogo.gridwidth = 2;
    gbc_lblLogo.insets = new Insets(0, 0, 5, 0);
    gbc_lblLogo.gridx = 0;
    gbc_lblLogo.gridy = 0;
    getContentPane().add(lblLogo, gbc_lblLogo);
    
    JLabel lblUsername = new JLabel("Username:");
    GridBagConstraints gbc_lblUsername = new GridBagConstraints();
    gbc_lblUsername.insets = new Insets(0, 0, 5, 5);
    gbc_lblUsername.anchor = GridBagConstraints.EAST;
    gbc_lblUsername.gridx = 0;
    gbc_lblUsername.gridy = 1;
    getContentPane().add(lblUsername, gbc_lblUsername);
    
    txtUsername = new JTextField();
    txtUsername.setText("Username");
    GridBagConstraints gbc_txtUsername = new GridBagConstraints();
    gbc_txtUsername.insets = new Insets(0, 0, 5, 0);
    gbc_txtUsername.fill = GridBagConstraints.HORIZONTAL;
    gbc_txtUsername.gridx = 1;
    gbc_txtUsername.gridy = 1;
    getContentPane().add(txtUsername, gbc_txtUsername);
    txtUsername.setColumns(10);
    
    JLabel lblPassword = new JLabel("Password:");
    GridBagConstraints gbc_lblPassword = new GridBagConstraints();
    gbc_lblPassword.insets = new Insets(0, 0, 5, 5);
    gbc_lblPassword.anchor = GridBagConstraints.EAST;
    gbc_lblPassword.gridx = 0;
    gbc_lblPassword.gridy = 2;
    getContentPane().add(lblPassword, gbc_lblPassword);
    
    pwdPassword = new JPasswordField();
    GridBagConstraints gbc_pwdPassword = new GridBagConstraints();
    gbc_pwdPassword.insets = new Insets(0, 0, 5, 0);
    gbc_pwdPassword.fill = GridBagConstraints.HORIZONTAL;
    gbc_pwdPassword.gridx = 1;
    gbc_pwdPassword.gridy = 2;
    getContentPane().add(pwdPassword, gbc_pwdPassword);
    
    btnBecomeABieber = new JButton("Become a Bieber Fans");
    GridBagConstraints gbc_btnBecomeABieber = new GridBagConstraints();
    gbc_btnBecomeABieber.insets = new Insets(0, 0, 5, 0);
    gbc_btnBecomeABieber.gridx = 1;
    gbc_btnBecomeABieber.gridy = 3;
    getContentPane().add(btnBecomeABieber, gbc_btnBecomeABieber);
    
    btnBecomeABieber.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
          LogButtonClicked();
      }
    });
    
    btnChangeSecretFolder = new JButton("Change Secret Folder");
    GridBagConstraints gbc_btnChangeSecretFolder = new GridBagConstraints();
    gbc_btnChangeSecretFolder.insets = new Insets(0, 0, 5, 0);
    gbc_btnChangeSecretFolder.gridx = 1;
    gbc_btnChangeSecretFolder.gridy = 4;
    getContentPane().add(btnChangeSecretFolder, gbc_btnChangeSecretFolder);
    
    btnChangeSecretFolder.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
          ChangeSecretFolderClicked();
      }
    });
    
    txtaLogInfo = new JTextArea();
    txtaLogInfo.setEditable(false);
    GridBagConstraints gbc_LogInfo = new GridBagConstraints();
    gbc_LogInfo.gridwidth = 2;
    gbc_LogInfo.fill = GridBagConstraints.BOTH;
    gbc_LogInfo.gridx = 0;
    gbc_LogInfo.gridy = 5;
    getContentPane().add(txtaLogInfo, gbc_LogInfo);
    
    init();
  }
  
  public void UpdateGUI() {
    switch (engine_.state()) {
      case kOnline:
        txtUsername.setEnabled(false);
        pwdPassword.setEnabled(false);
        btnBecomeABieber.setText("Quit being a Belieber!");        
        break;
      case kOffline:
        // can enter username, password, login, or change directory
        txtUsername.setEnabled(true);
        pwdPassword.setEnabled(true);
        btnBecomeABieber.setText("Become a Belieber!");
        break;
      case kServerUnreachable:
        txtUsername.setEnabled(false);
        pwdPassword.setEnabled(false);
        btnBecomeABieber.setText("Quit being a Belieber!");        
        break;
    }
  }
  
  public void LogButtonClicked() {
    Util.Log("Login/Logout Button Clicked!");
    if (engine_.state() == ClientEngine.State.kOffline) { 
      char[] pwd = pwdPassword.getPassword();
      StringBuilder pass = new StringBuilder();
      for (char c : pwd) pass.append(c);
      for (int i = 0; i < pwd.length; ++i) pwd[i] = 0;
      engine_.LogIn(txtUsername.getText(), pass.toString());
    } else {
      engine_.LogOut();
    }
    
    UpdateGUI();
  }
  
  public void ChangeSecretFolderClicked() {
    Util.Log("Change Secret Folder Clicked!");
    engine_.SetDirectory(JOptionPane.showInputDialog(
        "Please enter directory. Repository will be created at dir/biebox",
        "/home/dolphinigle/MT/KULIAH/PROGIn/tubes4"));
    UpdateGUI();
  }
  
  public void AddLog(String text) {
    String cur_text = txtaLogInfo.getText();
    cur_text = text + "\n" + cur_text;
    txtaLogInfo.setText(cur_text.substring(0, Math.min(1000, cur_text.length())));
  }
  
  public static void main(String[] args) {
    
    JFrame f = new ClientGUI();
    f.setVisible(true);
    
  }

}
